package com.nado.iims2.blog.action.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nado.iims2.blog.bo.manage.CodeBO;
import com.nado.iims2.blog.model.manage.Category;
import com.nado.iims2.blog.model.manage.Code;

/**
 * Handles requests for the application code page.
 */
@Controller
public class CodeController {
	private static final Logger logger = LoggerFactory.getLogger(CodeController.class);
	
	@Autowired
	private CodeBO codeBO = null;
	
	@RequestMapping(value = "/manage/category", method = RequestMethod.GET)
	public String category(Model model) {
		logger.info("Welcome manage/category!");
		List<Category> categoryList = codeBO.getCategoryList();
		
		model.addAttribute("categoryList", categoryList);
		return "manage/categoryList";
	}
	
	@RequestMapping(value = "/manage/code/{categoryCd}", method = RequestMethod.GET)
	public String code(@PathVariable("categoryCd") String categoryCd, Model model) {
		logger.info("Welcome manage/code/" + categoryCd);
		
		List<Code> codeList = codeBO.getCodeList(categoryCd);
		model.addAttribute("categoryCd", categoryCd);
		model.addAttribute("codeList", codeList);
		return "manage/codeList";
	}
	
	@RequestMapping(value = "/manage/category/form", method = RequestMethod.GET)
	public String categoryForm(Model model) {
		Category category = new Category();
		category.setRegist(true);
		model.addAttribute(category);
		return "manage/categoryForm";
	}
	
	@RequestMapping(value = "/manage/category/write", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> categoryWrite(
			@ModelAttribute Category category) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		category.setUserId("SYSTEM");
		
		int result = codeBO.writeCategory(category);
		
		logger.info("write category " + result);
		
		jsonMap.put("status", result);
		return jsonMap;
	}
	
	@RequestMapping(value = "/manage/code/form/{categoryCd}", method = RequestMethod.GET)
	public String codeForm(@PathVariable("categoryCd") String categoryCd, Model model) {
		Code codeModel = new Code();
		codeModel.setRegist(true);
		codeModel.setCategoryCd(categoryCd);
		
		model.addAttribute("codeModel", codeModel);
		return "manage/codeForm";
	}
	
	@RequestMapping(value = "/manage/code/write", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> codeWrite(@ModelAttribute Code code) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		code.setUserId("SYSTEM");
		
		int result = codeBO.writeCode(code);
		jsonMap.put("status", result);
		return jsonMap;
	}
	
	@RequestMapping(value = "/manage/category/check", method = RequestMethod.GET)
	public @ResponseBody Map<String, ? extends Object> categoryCheck(
			@RequestParam("categoryCd") String categoryCd) {
		
		logger.info("Welcome manage/category/check " + categoryCd);
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		int result = codeBO.getByCategoryCd(categoryCd);
		
		logger.info("result is " + result);
		
		jsonMap.put("status", result);
		return jsonMap;
	}
	
	@RequestMapping(value = "/manage/code/check", method = RequestMethod.GET)
	public @ResponseBody Map<String, ? extends Object> codeCheck(
			@RequestParam("categoryCd") String categoryCd,
			@RequestParam("code") String code) {
		logger.info("Welcome manage/code/check " + categoryCd + ", " + code);
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		int result = codeBO.getByCodeCd(categoryCd, code);
		
		logger.info("result is " + result);
		
		jsonMap.put("status", result);
		return jsonMap;
		
	}
}
